package com.example.test;

//import android.os.Bundle;
//import android.app.Activity;
//import android.view.Menu;
//
//
//import gov.nasa.worldwind.Model;
//import gov.nasa.worldwind.WorldWindowGLSurfaceView;
//import gov.nasa.worldwind.geom.Position;
//import gov.nasa.worldwind.layers.LayerList;
//import gov.nasa.worldwind.layers.RenderableLayer;
//import gov.nasa.worldwind.render.BasicShapeAttributes;
//import gov.nasa.worldwind.render.Color;
//import gov.nasa.worldwind.render.Path;
//import gov.nasa.worldwind.render.ShapeAttributes;
//import java.util.Arrays;
//
//public class MainActivity extends Activity {
//
//	@Override
////	protected void onCreate(Bundle savedInstanceState) {
////		super.onCreate(savedInstanceState);
////		setContentView(R.layout.activity_main);
////	}
//	public void onCreate(Bundle bundle)
//    {
//        super.onCreate(bundle);
//        RenderableLayer renderablelayer = new RenderableLayer();
//        Position aposition[] = new Position[2];
//        Position position = Position.fromDegrees(28D, -110D, 10000D);
//        aposition[0] = position;
//        Position position1 = Position.fromDegrees(35D, -108D, 10000D);
//        aposition[1] = position1;
//        java.util.List list = Arrays.asList(aposition);
//        Path path = new Path(list);
//        renderablelayer.addRenderable(path);
//        Position aposition1[] = new Position[2];
//        Position position2 = Position.fromDegrees(28D, -108D, 10000D);
//        aposition1[0] = position2;
//        Position position3 = Position.fromDegrees(35D, -106D, 10000D);
//        aposition1[1] = position3;
//        java.util.List list1 = Arrays.asList(aposition1);
//        Path path1 = new Path(list1);
//        path1.setAltitudeMode("gov.nasa.worldwind.avkey.RelativeToGround");
//        path1.setPathType("gov.nasa.worldwind.avkey.GreatCircle");
//        BasicShapeAttributes basicshapeattributes = new BasicShapeAttributes();
//        Color color = Color.randomColor();
//        basicshapeattributes.setOutlineColor(color);
//        basicshapeattributes.setOutlineWidth(4D);
//        path1.setAttributes(basicshapeattributes);
//        renderablelayer.addRenderable(path1);
//        Position aposition2[] = new Position[4];
//        Position position4 = Position.fromDegrees(28D, -106D, 40000D);
//        aposition2[0] = position4;
//        Position position5 = Position.fromDegrees(35D, -104D, 40000D);
//        aposition2[1] = position5;
//        Position position6 = Position.fromDegrees(35D, -102D, 40000D);
//        aposition2[2] = position6;
//        Position position7 = Position.fromDegrees(28D, -104D, 40000D);
//        aposition2[3] = position7;
//        java.util.List list2 = Arrays.asList(aposition2);
//        Path path2 = new Path(list2);
//        path2.setAltitudeMode("gov.nasa.worldwind.avkey.Absolute");
//        path2.setPathType("gov.nasa.worldwind.avkey.GreatCircle");
//        path2.setExtrude(true);
//        BasicShapeAttributes basicshapeattributes1 = new BasicShapeAttributes();
//        Color color1 = Color.randomColor();
//        basicshapeattributes1.setOutlineColor(color1);
//        Color color2 = Color.randomColor();
//        basicshapeattributes1.setInteriorColor(color2);
//        basicshapeattributes1.setOutlineWidth(4D);
//        path2.setAttributes(basicshapeattributes1);
//        renderablelayer.addRenderable(path2);
//        boolean flag = wwd.getModel().getLayers().add(renderablelayer);
//    }
//
//	
//
//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//		return true;
//	}
//
//}


import gov.nasa.worldwind.BasicView;
import gov.nasa.worldwind.Configuration;
import gov.nasa.worldwind.Model;
import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.WorldWindowGLSurfaceView;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.event.TapEvent;
import gov.nasa.worldwind.event.TapListener;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.geom.Rect;
import gov.nasa.worldwind.geom.Vec4;
import gov.nasa.worldwind.globes.Globe;
import gov.nasa.worldwind.layers.IconLayer;
import gov.nasa.worldwind.layers.Layer;
import gov.nasa.worldwind.layers.LayerList;
import gov.nasa.worldwind.layers.WorldMapLayer;
import gov.nasa.worldwind.pick.PickedObject;
import gov.nasa.worldwind.pick.PickedObjectList;
import gov.nasa.worldwind.render.UserFacingIcon;
import gov.nasa.worldwind.util.dashboard.DashboardView;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

/**
 * @author dcollins
 * @version $Id: WorldWindowActivity.java 83 2011-10-06 15:37:57Z ccrick $
 */
public class MainActivity extends Activity
{
    static
    {
        System.setProperty("gov.nasa.worldwind.app.config.document", "config/wwandroiddemo.xml");
    }

    protected WorldWindowGLSurfaceView wwd;
    protected DashboardView dashboard;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // TODO: temporary method of setting the location of the file store on Android. Need to replace this with
        // TODO: something more flexible.
        File fileDir = getFilesDir();
        System.setProperty("gov.nasa.worldwind.platform.user.store", fileDir.getAbsolutePath());

        this.setContentView(R.layout.main);

        this.wwd = (WorldWindowGLSurfaceView) this.findViewById(R.id.wwd);
        this.wwd.setModel((Model) WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME));
        this.addIconLayer();
        this.setupView();
        this.setupTextViews();

        // Link the Android Dashboard view to this activity's WorldWindow.
        this.dashboard = (DashboardView) this.findViewById(R.id.dashboard);
        this.dashboard.setWwd(this.wwd);
    }

    private void addIconLayer() {
		IconLayer iconLayer = new IconLayer();
		addIcon(iconLayer,Position.fromDegrees(0, 0, 0),"http://www.google.com");
		addIcon(iconLayer,Position.fromDegrees(0, -90, 0),"http://www.yahoo.com");
		addIcon(iconLayer,Position.fromDegrees(0, -180, 0),"http://www.amazon.com");
		addIcon(iconLayer,Position.fromDegrees(0, 90, 0),"http://www.msn.com");
		//insert before worldmaplayer
		LayerList layers = wwd.getModel().getLayers();
		int index = 0;
		for (Layer layer : layers){
			if (layer instanceof WorldMapLayer){
				break;
			}
			index++;
		}
		
		
		wwd.getModel().getLayers().add(index, iconLayer);
		wwd.getInputHandler().addTapListener(new TapListener() {
			@Override
			public void tapped(TapEvent event) {
				if (event.getEventType().equals(TapEvent.SINGLE_TAP)){
					PickedObjectList pickedObjects = wwd.getObjectsAtCurrentPosition();
					for (PickedObject po : pickedObjects){
						if (po.getObject() instanceof UserFacingIcon){
							String url = (String) ((UserFacingIcon)po.getObject()).getValue("url");
							if (url != null){
								Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
								startActivity(browserIntent);
							}
						}
					}
				}
			}
		});
	}

	private void addIcon(IconLayer iconLayer, Position pos, String url) {
		UserFacingIcon icon = new UserFacingIcon("images/32x32-icon-nasa.png", pos);
		icon.setToolTipText(url);
		icon.setShowToolTip(true);
		icon.setToolTipOffset(new Vec4(32,64,0));
		icon.setSize(new Rect(0,0,64,64));
		icon.setValue("url", url);
		iconLayer.addIcon(icon);
	}

	@Override
    protected void onPause()
    {
        super.onPause();

        // Pause the OpenGL ES rendering thread.
        this.wwd.onPause();
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        // Resume the OpenGL ES rendering thread.
        this.wwd.onResume();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Configure the application's options menu using the XML file res/menu/options.xml.
        this.getMenuInflater().inflate(R.menu.options, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Display the World Wind dashboard when the dashboard options menu item is selected.
        switch (item.getItemId())
        {
            case R.id.dashboard:
                this.dashboard.setVisibility(View.VISIBLE);
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    protected void setupView()
    {
        // TODO: this should be done during View initialization, not here in the application.
        BasicView view = (BasicView) this.wwd.getView();
        Globe globe = this.wwd.getModel().getGlobe();

        Position lookFromPosition = Position.fromDegrees(Configuration.getDoubleValue(AVKey.INITIAL_LATITUDE),
            Configuration.getDoubleValue(AVKey.INITIAL_LONGITUDE),
            Configuration.getDoubleValue(AVKey.INITIAL_ALTITUDE));
        view.setEyePosition(lookFromPosition, Angle.fromDegrees(0), Angle.fromDegrees(0), globe);
        view.setEyeTilt(Angle.fromDegrees(0), globe);
    }

    protected void setupTextViews()
    {
        TextView latTextView = (TextView) findViewById(R.id.latvalue);
        this.wwd.setLatitudeText(latTextView);
        TextView lonTextView = (TextView) findViewById(R.id.lonvalue);
        this.wwd.setLongitudeText(lonTextView);
    }
}


